package com.moss.system.service.impl;

import com.moss.common.constant.Constants;
import com.moss.common.util.TokenUtil;
import com.moss.common.entity.SysUserEntity;
import com.moss.system.mapper.UserMapper;
import com.moss.system.service.MenuService;
import com.moss.system.service.PermissionService;
import com.moss.system.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author gxcc3
 * @date: 2024/8/22 23:10
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    RoleService roleService;

    @Resource
    MenuService menuService;

    @Resource
    UserMapper userMapper;

    @Override
    public Set<String> getRolePermissionList(SysUserEntity user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (TokenUtil.isAdmin(user.getUserId(), Constants.ADMIN_KEY))
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    @Override
    public Set<String> getMenuPermissionList(SysUserEntity user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (TokenUtil.isAdmin(user.getUserId(), Constants.ADMIN_KEY))
        {
            perms.add("*:*:*");
        }
        else
        {
            perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
        }
        return perms;
    }
}
